//
//  BKACalendarHeaderReuableView.m
//  TestCollectionView
//
//  Created by Lemon on 2019/4/20.
//  Copyright © 2019 sunshinfight@163.com. All rights reserved.
//

#import "JXCalendarMonthHeaderView.h"
#import "NSCalendar+JXCoveniences.h"

@implementation JXCalendarMonthHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configUI];
    }
    return self;
}

- (void)configUI
{
    [self addSubview:self.dateLabel];
    self.dateLabel.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.dateLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.dateLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.dateLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
    NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self.dateLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    [self addConstraints: @[left, right, top, bottom]];
}

- (UILabel *)dateLabel
{
    if (_dateLabel == nil) {
        _dateLabel = [UILabel new];
        _dateLabel.backgroundColor = UIColor.whiteColor;
        _dateLabel.font = [UIFont systemFontOfSize:18];
        _dateLabel.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
    }
    return _dateLabel;
}

- (void)configWithModel: (JXCalendarModel*)model
{
    NSDate *today = NSDate.date;
    int32_t year = [NSCalendar yearWithDate:today];
    NSString *labText = [NSString stringWithFormat: @"%d月", model.month];
    if (year != model.year) {
        labText = [NSString stringWithFormat:@"%d年%d月", model.year, model.month];
    }
    self.dateLabel.text = labText;
    
}
@end
